﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Commands;
using PloobsEngine.Features;
using PloobsEngine.Input;
using PloobsEngine.Light;
using PloobsEngine.Material;
using PloobsEngine.Modelo;
using PloobsEngine.Physics;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.SceneControl;
using PloobsEngineDemo3;

namespace Etapa1.Screens
{
    public class AnimatedTextureScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;

        public AnimatedTextureScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);                                    
        }
        
        public override void LoadContent(EngineStuff es)
        {            
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            base.LoadContent(es);
            
            
            #region Models
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);                
                CustomDeferred shader = new CustomDeferred(false, false, false, false);
               
                shader.Initialize();
                IMaterial mat = new Material(shader);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }

            {
                Matrix[] world = new Matrix[10 * 10];
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        float x, y;
                        x = i * 5;
                        y = j * 5;
                        Matrix m = Matrix.CreateWorld(new Vector3(x, 20, y), Vector3.Right, Vector3.Up);                        
                        world[i * 10 + j] = m;
                    }
                }
                ///Create the model of the bilboard (lots of points in this implementation)
                ///World is the Transformation matrix (Center of the bilboard in world coordinates) and wizard is the animated texture (large texture with the animation frames - frames are put side by side, horizontal )
                OrientedPlaneModel bm = new OrientedPlaneModel(ref world, "..\\Content\\Textures\\wizard");
                ///Bilboard Animation Shader                
                AnimatedTextureShader cb = new AnimatedTextureShader(3,100,BilboardType.Cilindric);
                cb.Amplitude = 0f;
                cb.Scale = new Vector2(5, 5);
                cb.Atenuation = new Vector4(0.4f, 0.4f, 0.4f, 1);
                Material matfor = new Material(cb);
                ///Do not have physical entity
                IObject obj2 = new SimpleObject(matfor, new DummyPhysicObject(), bm);
                mundo.AddObject(obj2);
            }                    


            #endregion            

            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 3000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);         

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion

            
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            dr.isDebug = true;
            mundo.AddCamera(cam);
            this.World = mundo;
        }
    }
}

